package com.mdnote.jianzhi_offer.linked_list;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/24
 * @description 删除有序链表的重复元素
 */
public class Offer18II {

    public static void main(String[] args) {
        Offer18II offer18II = new Offer18II();

        ListNode node = new ListNode(11);
        node.next = new ListNode(12);
        node.next.next = new ListNode(12);
        node.next.next.next = new ListNode(12);
        node.next.next.next.next = new ListNode(13);
        node.next.next.next.next.next = new ListNode(13);

        offer18II.delete(node);

        ListNode cur = node;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public ListNode delete(ListNode head) {
        ListNode cur = head;

        while (cur != null) {
            while (cur.next != null && cur.next.next != null && cur.next.val == cur.next.next.val) {
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }

        return head;
    }

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}
